package com.egame.manage.biz.web.controller.cu;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.egame.common.model.PageData;
import cn.egame.common.servlet.WebUtils;
import cn.egame.ext.cu.ManufactureInfo;
import com.egame.manage.biz.server.ManageClientExt;
import com.egame.manage.biz.server.cu.ManufacturerServiceImpl;
import com.egame.manage.biz.web.controller.BaseController;
import com.egame.manage.biz.domain.ManufactureBean;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/tv/manufacturer")
public class NewManufacturerController  extends BaseController {
	
	public NewManufacturerController(){		
	}
	public NewManufacturerController(HttpServletRequest req,
			HttpServletResponse resp) {
		super(req, resp);
	}

	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})		
	public ModelAndView listManufacturer(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int page = WebUtils.getInt(req,"page",0);
		int rowsOfPage = WebUtils.getInt(req,"rowsOfPage",15);
		String nameSearch = WebUtils.getString(req, "nameSearch","");
		String shortNameSearch = WebUtils.getString(req, "shortNameSearch","");
		String enNameSearch = WebUtils.getString(req, "enNameSearch","");
		String enShortSearch = WebUtils.getString(req, "enShortSearch","");
		
		ManufactureInfo bean = new ManufactureInfo();
		bean.setCnName(nameSearch);
		bean.setShortName(shortNameSearch);
		bean.setEnName(enNameSearch);
		bean.setEnShort(enShortSearch);
		PageData pd = ManufacturerServiceImpl.getInstance().pageManufacture(appId, rowsOfPage, bean, page, rowsOfPage);
		ModelAndView mav = new ModelAndView("/new/op/manufacturerList"); 
		mav.addObject("pd", pd);
		mav.addObject("pdUrl", "/tv/manufacturer/list");
		mav.addObject("nameSearch", nameSearch);
		mav.addObject("shortNameSearch", shortNameSearch);
		mav.addObject("enNameSearch", enNameSearch);
		mav.addObject("enShortSearch", enShortSearch);
		return mav;
	}

	@RequestMapping(value = "/set", method = {RequestMethod.GET,RequestMethod.POST})		
	public ModelAndView setManufacturer(ManufactureBean item,HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
			item.setOperator(userBean.getOperatorId());
		}
		ManufacturerServiceImpl.getInstance().setManufactureInfo(appId,loginUserId,item);		
		return listManufacturer(req,resp);
	}
	
	@RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})			
	public ModelAndView deleteManufacturer(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int manufacturerId = WebUtils.getInt(req,"manufacturerId",0);		
		ManufacturerServiceImpl.getInstance().deleteManufacturer(appId,loginUserId,manufacturerId);
		return listManufacturer(req,resp);
	}

	@RequestMapping(value = "/edit/show", method = {RequestMethod.GET,RequestMethod.POST})			
	public ModelAndView showEditManufacturer(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int manufacturerId = WebUtils.getInt(req,"manufacturerId",0);		
		ManufactureInfo bean = ManageClientExt.getInstance().getManufactureInfoById(appId, loginUserId, manufacturerId);
		ModelAndView mav = new ModelAndView("/new/op/editManufacturer"); 
		mav.addObject("bean", bean);
		return mav;
	}	

	@RequestMapping(value = "/edit", method = {RequestMethod.GET,RequestMethod.POST})			
	public ModelAndView editManufacturer(ManufactureInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ManufacturerServiceImpl.getInstance().updateManufactureInfo(appId, loginUserId, bean);
		return listManufacturer(req,resp);
	}
	
}
